{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.pylab as pylab\n",
    "import matplotlib.cm as cm\n",
    "%matplotlib inline\n",
    "import scipy.misc\n",
    "import Image\n",
    "import scipy.io\n",
    "import os\n",
    "\n",
    "\n",
    "# Make sure that caffe is on the python path:\n",
    "caffe_root = '../../../'\n",
    "import sys\n",
    "sys.path.insert(0, caffe_root + 'python')\n",
    "\n",
    "import caffe\n",
    "\n",
    "EPSILON = 1e-8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data_root = '/opt/dataset/saliency/msra/Imgs/'\n",
    "with open('../../../data/msra_b/test.lst') as f:\n",
    "    test_lst = f.readlines()\n",
    "    \n",
    "test_lst = [data_root+x.strip() for x in test_lst]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#remove the following two lines if testing with cpu\n",
    "caffe.set_mode_gpu()\n",
    "# choose which GPU you want to use\n",
    "caffe.set_device(0)\n",
    "caffe.SGDSolver.display = 0\n",
    "# load net\n",
    "net = caffe.Net('deploy.prototxt', 'dss_model_released.caffemodel', caffe.TEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Visualization\n",
    "def plot_single_scale(scale_lst, name_lst, size):\n",
    "    pylab.rcParams['figure.figsize'] = size, size/2\n",
    "    plt.figure()\n",
    "    for i in range(0, len(scale_lst)):\n",
    "        s = plt.subplot(1,5,i+1)\n",
    "        s.set_xlabel(name_lst[i], fontsize=10)\n",
    "        if name_lst[i] == 'Source':\n",
    "            plt.imshow(scale_lst[i])\n",
    "        else:\n",
    "            plt.imshow(scale_lst[i], cmap = cm.Greys_r)\n",
    "        s.set_xticklabels([])\n",
    "        s.set_yticklabels([])\n",
    "        s.yaxis.set_ticks_position('none')\n",
    "        s.xaxis.set_ticks_position('none')\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "idx = 39\n",
    "\n",
    "# load image\n",
    "img = Image.open(test_lst[idx])\n",
    "img = np.array(img, dtype=np.uint8)\n",
    "im = np.array(img, dtype=np.float32)\n",
    "im = im[:,:,::-1]\n",
    "im -= np.array((104.00698793,116.66876762,122.67891434))\n",
    "im = im.transpose((2,0,1))\n",
    "\n",
    "# load gt\n",
    "gt = Image.open(test_lst[idx][:-4] + '.png')\n",
    "\n",
    "# shape for input (data blob is N x C x H x W), set data\n",
    "net.blobs['data'].reshape(1, *im.shape)\n",
    "net.blobs['data'].data[...] = im\n",
    "# run net and take argmax for prediction\n",
    "net.forward()\n",
    "out1 = net.blobs['sigmoid-dsn1'].data[0][0,:,:]\n",
    "out2 = net.blobs['sigmoid-dsn2'].data[0][0,:,:]\n",
    "out3 = net.blobs['sigmoid-dsn3'].data[0][0,:,:]\n",
    "out4 = net.blobs['sigmoid-dsn4'].data[0][0,:,:]\n",
    "out5 = net.blobs['sigmoid-dsn5'].data[0][0,:,:]\n",
    "out6 = net.blobs['sigmoid-dsn6'].data[0][0,:,:]\n",
    "fuse = net.blobs['sigmoid-fuse'].data[0][0,:,:]\n",
    "res = (out3 + out4 + out5 + fuse) / 4\n",
    "res = (res - np.min(res) + EPSILON) / (np.max(res) - np.min(res) + EPSILON)\n",
    "out_lst = [out1, out2, out3, out4, out5]\n",
    "name_lst = ['SO1', 'SO2', 'SO3', 'SO4', 'SO5']\n",
    "plot_single_scale(out_lst, name_lst, 10)\n",
    "out_lst = [out6, fuse, res, img, gt]\n",
    "name_lst = ['SO6', 'Fuse', 'Result', 'Source', 'GT']\n",
    "plot_single_scale(out_lst, name_lst, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
